﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Net.Mail;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;

namespace AgileShare.Framework.Library.Helpers
{
    /// <summary>
    /// This class contains helper methods for drafting/sending Emails through SharePoint
    /// - All methods should be static
    /// - DO NOT USE SPContext
    /// </summary>
    public class EmailHelper
    {
        /// <summary>
        /// Send an email containing the details of an Exception
        /// </summary>
        /// <param name="web"></param>
        /// <param name="ex"></param>
        /// <param name="additionalMessage"></param>
        public static void SendErrorEmail(SPWeb web, Exception ex, string additionalMessage)
        {
            System.Text.StringBuilder body = new System.Text.StringBuilder("Error: ");
            body.Append(additionalMessage);
            body.Append("<br><br>");
            SPUtility.SendEmail(web, true, true, "developers", "AgileShare.Framework Error", body.ToString());
        }

        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="replacementTokens">The replacement tokens.</param>
        /// <param name="replacementValues">The replacement values.</param>
        public static void SendEmail(MailMessage msg, string[] replacementTokens, string [] replacementValues)
        {
            try
            {
                // Get SharePoint’s default email configuration entries
                string smtpServer = SPAdministrationWebApplication.Local
                  .OutboundMailServiceInstance.Server.Address;
                string smtpFrom = SPAdministrationWebApplication.Local
                  .OutboundMailSenderAddress;
                string smtpReplyTo = SPAdministrationWebApplication.Local
                  .OutboundMailReplyToAddress;

                //set up email message
                SmtpClient smtpClient = new SmtpClient(smtpServer);
                msg.Body = DoReplacements(msg.Body, replacementTokens, replacementValues);
                msg.Subject = DoReplacements(msg.Subject, replacementTokens, replacementValues);

                smtpClient.Send(msg);
            }
            catch { }
        }

        /// <summary>
        /// Does the replacements on the Tokenized string.
        /// </summary>
        /// <param name="start">The start.</param>
        /// <param name="tokens">The tokens.</param>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        private static string DoReplacements(string start, string[] tokens, string[] values)
        {
            string retVal = start;

            if (tokens.Length == values.Length)
            {
                for (int i = 0; i < tokens.Length; i++)
                {
                    retVal = retVal.Replace(tokens[i], values[i]);
                }
            }
            else
            {
                throw (new Exception("Array length of tokens and values must match"));
            }

            return retVal;
        }

        /// <summary>
        /// Send Email
        /// </summary>
        /// <param name="body"></param>
        /// <param name="subject"></param>
        /// <param name="sendtoaddress"></param>
        /// <param name="replytoaddress"></param>
        public static void SendEmail(string body, string subject, string sendtoaddress, string replytoaddress)
        {
            try
            {
                // Get SharePoint’s default email configuration entries
                string smtpServer = SPAdministrationWebApplication.Local
                  .OutboundMailServiceInstance.Server.Address;
                string smtpFrom = SPAdministrationWebApplication.Local
                  .OutboundMailSenderAddress;
                string smtpReplyTo = SPAdministrationWebApplication.Local
                  .OutboundMailReplyToAddress;

                //set up email message
                SmtpClient smtpClient = new SmtpClient(smtpServer);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(smtpFrom);
                msg.To.Add(new MailAddress(sendtoaddress));
                if (!String.IsNullOrEmpty(replytoaddress))
                {
                    msg.ReplyTo = new MailAddress(replytoaddress);
                }
                else
                {
                    msg.ReplyTo = new MailAddress(smtpReplyTo);
                }
                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = false;
                smtpClient.Send(msg);
            }
            catch { }
        }

        /// <summary>
        /// Send Email
        /// </summary>
        /// <param name="body"></param>
        /// <param name="subject"></param>
        /// <param name="sendtoaddress"></param>
        /// <param name="replytoaddress"></param>
        /// <param name="IsHTML"></param>
        public static void SendEmail(string body, string subject, string sendtoaddress, string replytoaddress, bool IsHTML)
        {
            try
            {
                // Get SharePoint’s default email configuration entries
                string smtpServer = SPAdministrationWebApplication.Local
                  .OutboundMailServiceInstance.Server.Address;
                string smtpFrom = SPAdministrationWebApplication.Local
                  .OutboundMailSenderAddress;
                string smtpReplyTo = SPAdministrationWebApplication.Local
                  .OutboundMailReplyToAddress;

                //set up email message
                SmtpClient smtpClient = new SmtpClient(smtpServer);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(smtpFrom);
                msg.To.Add(new MailAddress(sendtoaddress));
                if (!String.IsNullOrEmpty(replytoaddress))
                {
                    msg.ReplyTo = new MailAddress(replytoaddress);
                }
                else
                {
                    msg.ReplyTo = new MailAddress(smtpReplyTo);
                }
                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = IsHTML;
                smtpClient.Send(msg);
            }
            catch { }
        }

        /// <summary>
        /// Send Email
        /// </summary>
        /// <param name="body"></param>
        /// <param name="subject"></param>
        /// <param name="sendtoaddress"></param>
        /// <param name="replytoaddress"></param>
        /// <param name="IsHTML"></param>
        /// <param name="messageAttachments"></param>
        public static void SendEmail(string body, string subject, string sendtoaddress, string replytoaddress, bool IsHTML, AttachmentCollection messageAttachments)
        {
            try
            {
                // Get SharePoint’s default email configuration entries
                string smtpServer = SPAdministrationWebApplication.Local
                  .OutboundMailServiceInstance.Server.Address;
                string smtpFrom = SPAdministrationWebApplication.Local
                  .OutboundMailSenderAddress;
                string smtpReplyTo = SPAdministrationWebApplication.Local
                  .OutboundMailReplyToAddress;

                //set up email message
                SmtpClient smtpClient = new SmtpClient(smtpServer);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(smtpFrom);
                msg.To.Add(new MailAddress(sendtoaddress));
                if (!String.IsNullOrEmpty(replytoaddress))
                {
                    msg.ReplyTo = new MailAddress(replytoaddress);
                }
                else
                {
                    msg.ReplyTo = new MailAddress(smtpReplyTo);
                }
                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = IsHTML;
                foreach (Attachment att in messageAttachments)
                {
                    msg.Attachments.Add(att);
                }
                smtpClient.Send(msg);
            }
            catch { }
        }

    }
}
